var http = require('http')
var fs = require('fs')

var server = http.createServer()

var wwwDir = 'E:/documents/QQ'

server.on('request', function(req, resp) {
  var url = req.url
   
  fs.readFile('./template.html', function(err, data) {
	if (err) {
	  return resp.end('404 Not Found.')
	}
	
	fs.readdir(wwwDir, function(err, files) {
      if (err) {
	    return resp.end('File Not Found.')
	  }
	  var content = ''
	  files.forEach(function(item) {
	    content += `
		  <tr>
		    <td data-value="价格表.xls"><a class="icon file" draggable="true" href="file:///E:/documents/QQ/%E4%BB%B7%E6%A0%BC%E8%A1%A8.xls">${item}</a></td>
		    <td class="detailsColumn" data-value="12288">12.0 kB</td>
		    <td class="detailsColumn" data-value="1596876551">2020/8/8 下午4:49:11</td>
		  </tr>
	    `
	  })
	  
	  data = data.toString()
	  data = data.replace('^_^', content)
	  resp.end(data)
    })
  })
})

server.listen(3000, function() {
  console.log('running...')
})